/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package poi.engine;

import dto.ExcelDTO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

/**
 *
 * @author techworld
 */
public class ExcelEngine {
    private int rowNum = 0;
    private FileInputStream file;
    private HSSFWorkbook workbook;
    private HSSFSheet sheet;
    private Row row;
    private Cell cell;
    
    public ExcelEngine(){
       
    }
    
    public void doReadExcelFile(){
        try{
            System.out.println("Inside: doReadExcelFile: "+ExcelDTO.getFilePath());
                        
            file = new FileInputStream(ExcelDTO.getFilePath());
            workbook = new HSSFWorkbook(file);
            sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            
            while(rowIterator.hasNext()) {
                row = rowIterator.next();
                                
                Iterator<Cell> cellIterator = row.cellIterator();
                while(cellIterator.hasNext()) {
             
                cell = cellIterator.next();
             
                    switch(cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t\t");
                            break;
                    }
                }
                ExcelDTO.setRowNum(row.getRowNum()+1);
                System.out.println("");
            }
            System.out.println("Row Num: "+ExcelDTO.getRowNum());
            file.close();   
        }
        catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (IOException e) {
             System.out.println(e.getMessage());
        }
    }
    
    public void doWriteExcelFile(){
        System.out.println("Entered into doWriteExcelFile ::: "+ExcelDTO.getRowNum()+":::::"+ExcelDTO.getDivNum());
        
        String fileNameWithOutExt = FilenameUtils.removeExtension(ExcelDTO.getFileName());
        String filename = ExcelDTO.getFileParentPath()+"/"+fileNameWithOutExt+"1.xls";
        int numLoop = 0;
        int restData = 0;
        int rowNum = ExcelDTO.getRowNum();
        int divNum = ExcelDTO.getDivNum();
        FileOutputStream outFile;
        
        try {      
            if((rowNum%divNum)==0)
            {
                numLoop = rowNum/divNum;

                System.out.println("Creating file: "+filename);
                outFile = new FileOutputStream(new File(filename));
                for(int i=1;i<=numLoop;i++){
                    sheet = workbook.createSheet(i+"");
                    for(int j=1; j<=divNum; j++){
                        //workbook.write(outFile);
                    }
                }
                outFile.close();
            }
            else{
                numLoop = rowNum/divNum;
                restData= rowNum%divNum;
                for(int i=1;i<=numLoop+1;i++){
                    sheet = workbook.createSheet(i+"");
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
